package com.pda.xinma.presenter;

import com.pda.xinma.bean.PickInfoBean;
import com.pda.xinma.bean.ScanResultBean;
import com.pda.xinma.bean.UserBean;
import com.pda.xinma.bean.request.PickingRequest;
import com.pda.xinma.bean.request.QRcodeRequest;
import com.pda.xinma.contract.PickingContract;
import com.pda.xinma.net.NetUtils;
import com.wangshen.base.net.bean.BaseAppEntity;
import com.wangshen.base.ui.mvp.base.presenter.BasePresenter;

import io.reactivex.functions.Consumer;

public class PickingPresenter extends BasePresenter<PickingContract.View> implements PickingContract.Presenter{
    @Override
    public void scanTheQRCodeToVerify(String code) {
        QRcodeRequest request = new QRcodeRequest();
        request.setQRCode(code);
        NetUtils.geApi().scanTheQRCodeToVerify(request)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<ScanResultBean>>() {
                    @Override
                    public void accept(BaseAppEntity<ScanResultBean> scanResultBeanBaseAppEntity) throws Exception {
                        getView().setScanBean(scanResultBeanBaseAppEntity.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }

    @Override
    public void confirmPick(PickingRequest request) {
        NetUtils.geApi().confirmPick(request)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<UserBean>>() {
                    @Override
                    public void accept(BaseAppEntity<UserBean> userBeanBaseAppEntity) throws Exception {
                        getView().showMessage(userBeanBaseAppEntity.getMessage());
                        getView().setSuccess();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }

    @Override
    public void confirmBack(PickingRequest request) {
        NetUtils.geApi().confirmBack(request)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<UserBean>>() {
                    @Override
                    public void accept(BaseAppEntity<UserBean> userBeanBaseAppEntity) throws Exception {
                        getView().showMessage(userBeanBaseAppEntity.getMessage());
                        getView().setSuccess();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }

    @Override
    public void getPickInfoByOrderAndBin(String orderNo, String connectionCode) {
        NetUtils.geApi().getPickInfoByOrderAndBin(orderNo, connectionCode)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<PickInfoBean>>() {
                    @Override
                    public void accept(BaseAppEntity<PickInfoBean> pickInfoBeanBaseAppEntity) throws Exception {
                        getView().setData(pickInfoBeanBaseAppEntity.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }
}

